/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud;

import com.comut.crud.ejb.interfaces.remote.IPoiActionRemote;
import com.comut.crud.pojo.Response;
import com.comut.crud.pojo.poi.Poi;
import com.comut.lib.error.ErrorCodes;
import com.comut.lib.exception.BaseException;
import java.util.ResourceBundle;
import javax.ejb.EJBException;
import javax.naming.InitialContext;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import org.apache.log4j.Logger;

/**
 * CRUD for Poi
 *
 * @author asantos
 */
@Path("Poi")
public class PoiResource {

    private static Logger _logger = org.apache.log4j.Logger.getLogger(PoiResource.class.getName());
    final String jndiName = ResourceBundle.getBundle("jndi").getString("poiBeanRemote");
    @Context
    private UriInfo context;

    /**
     * Creates a new instance of PoiResource
     */
    public PoiResource() {
    }

    /**
     * Retrieves representation of an instance of com.comut.crud.PoiResource
     *
     * @return an instance of java.lang.String
     */
    /*
     * @GET @Produces("application/json") public String getJson() { //TODO
     * return proper representation object throw new
     * UnsupportedOperationException();
    }
     */
    /**
     * PUT method for updating an instance of PoiResource
     *
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/json")
    public Response putJson(String content) {
        Response tr = new Response();
        try {
            _logger.info("PUT " + content.toString());
            javax.naming.Context ic = new InitialContext();
            Object obj = ic.lookup(jndiName);
            IPoiActionRemote idar = (IPoiActionRemote) obj;
            idar.updatePoi(content);
            _logger.info("PUT DONE");
            tr.setSuccess(true);

        } catch (BaseException ex) {
            tr.setSuccess(false);
            tr.setMessageError(ex.getErrorCode() + ", " + ex.getMessage());
            tr.setErrorCode(ex.getErrorCode());
            _logger.error(ex);
        } catch (EJBException ex) {

            tr.setSuccess(false);
            if (ex.getCausedByException() instanceof BaseException) {
                BaseException be = (BaseException) ex.getCausedByException();
                tr.setMessageError(be.getErrorCode() + ", " + be.getMessage());
                tr.setErrorCode(be.getErrorCode());
            } else {
                tr.setMessageError(ex.getMessage());
                tr.setErrorCode(ErrorCodes.GENERAL_ERROR);
            }
            _logger.error(ex);

        } catch (Exception ex) {
            tr.setSuccess(false);
            tr.setMessageError(ex.getMessage());
            tr.setErrorCode(ErrorCodes.GENERAL_ERROR);
            _logger.error(ex);
        }
        return tr;
    }

    /**
     * Post method for creating an instance of PoiResource
     *
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @POST
    @Consumes("application/json; charset=UTF-8")
    public Response postJson(Poi content) {
        Response tr = new Response();
        try {
            _logger.info("post " + content.toString());
            javax.naming.Context ic = new InitialContext();
            Object obj = ic.lookup(jndiName);
            IPoiActionRemote idar = (IPoiActionRemote) obj;
            Poi s = idar.insertPoi(content);
            tr.setSuccess(true);
            tr.setRetVal(s);
            _logger.info("POST DONE");

        } catch (BaseException ex) {
            tr.setSuccess(false);
            tr.setMessageError(ex.getErrorCode() + ", " + ex.getMessage());
            tr.setErrorCode(ex.getErrorCode());
            _logger.error(ex);
        } catch (EJBException ex) {

            tr.setSuccess(false);
            if (ex.getCausedByException() instanceof BaseException) {
                BaseException be = (BaseException) ex.getCausedByException();
                tr.setMessageError(be.getErrorCode() + ", " + be.getMessage());
                tr.setErrorCode(be.getErrorCode());
            } else {
                tr.setMessageError(ex.getMessage());
                tr.setErrorCode(ErrorCodes.GENERAL_ERROR);
            }
            _logger.error(ex);

        } catch (Exception ex) {
            tr.setSuccess(false);
            tr.setMessageError(ex.getMessage());
            tr.setErrorCode(ErrorCodes.GENERAL_ERROR);
            _logger.error(ex);
        }
        return tr;
    }

    @DELETE
    @Consumes("application/json; charset=UTF-8")
    public Response deleteJson(Poi content) {
        Response tr = new Response();
        try {
            _logger.info("DELETE " + content.toString());
            javax.naming.Context ic = new InitialContext();
            Object obj = ic.lookup(jndiName);
            IPoiActionRemote idar = (IPoiActionRemote) obj;
            idar.deletePoi(content);
            _logger.info("DELETE DONE");

            tr.setSuccess(true);

        } catch (BaseException ex) {
            tr.setSuccess(false);
            tr.setMessageError(ex.getErrorCode() + ", " + ex.getMessage());
            tr.setErrorCode(ex.getErrorCode());
            _logger.error(ex);
        } catch (EJBException ex) {

            tr.setSuccess(false);
            if (ex.getCausedByException() instanceof BaseException) {
                BaseException be = (BaseException) ex.getCausedByException();
                tr.setMessageError(be.getErrorCode() + ", " + be.getMessage());
                tr.setErrorCode(be.getErrorCode());
            } else {
                tr.setMessageError(ex.getMessage());
                tr.setErrorCode(ErrorCodes.GENERAL_ERROR);
            }
            _logger.error(ex);

        } catch (Exception ex) {
            tr.setSuccess(false);
            tr.setMessageError(ex.getMessage());
            tr.setErrorCode(ErrorCodes.GENERAL_ERROR);
            _logger.error(ex);
        }
        return tr;
    }
}
